﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace DAO
{
    public class Tho_DAO
    {
        private static Tho_DAO m_instance = null;

        public static Tho_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new Tho_DAO();
                return m_instance;
            }
        }

        /// <summary>
        ///   lấy thông tin thợ theo id
        /// </summary>
        /// 

        public DataTable LayTho(string id)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYTHOBYID", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaTho", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaTho"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;

            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   lấy toàn bộ ma thợ
        /// </summary>
        /// 
        public DataTable LayToanBoMaTho()
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYTOANBOMATHO", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   lay thong tin toan bo tho
        /// </summary>
        /// 
        public DataTable LayToanBoTho()
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYTOANBOTHO", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   tìm thông tin thợ
        /// </summary>
        /// 
        public DataTable TimKiemTho(string _id, string _name, string _address)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("FIND_THO", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaTho", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaTho"].Value = _id;

                cmd.Parameters.Add("@TenTho", SqlDbType.NVarChar, 40);
                cmd.Parameters["@TenTho"].Value = _name;

                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 100);
                cmd.Parameters["@DiaChi"].Value = _address;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;	
            }
        }

        /// <summary>
        ///   cap nhat tho
        /// </summary>
        /// 
        public bool CapNhatThongTinTho(string id, string name, string address)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("UPDATE_THO",cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaTho", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaTho"].Value = id;
                cmd.Parameters.Add("@TenTho", SqlDbType.NVarChar, 40);
                cmd.Parameters["@TenTho"].Value = name;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 100);
                cmd.Parameters["@DiaChi"].Value = address;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        ///   Them 1 tho moi
        /// </summary>
        /// 
        public bool ThemThoMoi(string _name, string _address)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_THO_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@TenTho", SqlDbType.NVarChar, 40);
                cmd.Parameters["@TenTho"].Value = _name;

                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 100);
                cmd.Parameters["@DiaChi"].Value = _address;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
            
        }

        /// <summary>
        ///   xoa thong tin tho
        /// </summary>
        /// 

        public bool XoaThongTinTho(string id)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("REMOVE_THO", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaTho", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaTho"].Value = id;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }
}
